package cn.shenchao.jms;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;


public class JMSProducer {

	public static void main(String[] args) throws Exception {
		// 链接工厂
		ConnectionFactory connectionFactory = null;
		// 连接
		Connection connection;
		// 会话接收或者发送消息的线程
		Session session;
		// 消息目的地
		Destination destination;
		// 消息生产者
		MessageProducer messageProducer;
		connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
				ActiveMQConnection.DEFAULT_PASSWORD, ActiveMQConnection.DEFAULT_BROKER_URL);
		connection = connectionFactory.createConnection();
		connection.start();
		session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
		destination = session.createQueue("FirstQueue1");
		messageProducer = session.createProducer(destination);
		sendMessage(session, messageProducer);
		session.commit();
		if (connection!=null) {
			connection.close();
		}
	}

	public static void sendMessage(Session session, MessageProducer messageProducer) throws JMSException {
		for (int i = 0; i < 10; i++) {
			TextMessage createTextMessage = session.createTextMessage("ActiveMQ 发送的消息"+i);
			System.out.println("发送消息:"+i);
			messageProducer.send(createTextMessage);
		}
	}
}
